﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using KeaoBase;
using GoldPrinter;
using System.Diagnostics;

namespace KeaoMDS.Report
{
    public partial class RiZhi : BaseForm
    {
        public RiZhi()
        {
            InitializeComponent();
        }

        private void RiZhi_Load(object sender, EventArgs e)
        {
            grid_rizhi.AutoGenerateColumns = false;
            dateTimePicker1.Value = DateTime.Today;
            dateTimePicker2.Value = dateTimePicker1.Value.AddHours(23).AddMinutes(59).AddSeconds(59);
            BindInfo();
        }

        private void BindInfo()
        {
            string createCondition = KeaoUtils._DB.CreateAndCondition("rizhishijian", DataValueCommonType.日期时间, dateTimePicker1.Value.ToString("yyyy-MM-dd HH:mm:ss"), dateTimePicker2.Value.ToString("yyyy-MM-dd HH:mm:ss"), SearchConditionType.between);
            DataTable Dt = KeaoUtils._DB.GetDataTableByCondition("tbl_rizhi", createCondition);
            grid_rizhi.DataSource = Dt;
            grid_rizhi.AutoResizeColumn(2);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            BindInfo();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            DataTable Dt = grid_rizhi.DataSource as DataTable;
            if (Dt == null)
            {
                return;
            }

            ExcelAccess excelAccess = new ExcelAccess();
            try
            {
                excelAccess.Open(Application.StartupPath + "\\Template\\日志.xls");
                excelAccess.SetCellText(2, "A", string.Format("报表时间：{0}——{1}", dateTimePicker1.Value, dateTimePicker2.Value));
                for (int i = 0; i < Dt.Rows.Count; i++)
                {
                    excelAccess.SetCellText(i + 4, "A", string.Format("{0}", Dt.Rows[i]["caozuoyuan"]));
                    excelAccess.SetCellText(i + 4, "B", string.Format("{0}", Dt.Rows[i]["rizhishijian"]));
                    excelAccess.SetCellText(i + 4, "C", string.Format("{0}", Dt.Rows[i]["rizhineirong"]));
                }

                excelAccess.MergeCells(4 + Dt.Rows.Count, "A", 4 + Dt.Rows.Count, "C", true);
                excelAccess.SetCellText(4 + Dt.Rows.Count, "A", string.Format("制表人：{0}    制表时间：{1}", KeaoUtils._CurrentUserRow["yonghuxingming"], DateTime.Now.ToString("yyyy-MM-dd HH:mm")));

                Excel.Range r = excelAccess.GetRange(4, "A", Dt.Rows.Count + 3, "C");
                excelAccess.SetBordersEdge(r, GoldPrinter.ExcelConstants.BordersEdge.xlLeft);
                excelAccess.SetBordersEdge(r, GoldPrinter.ExcelConstants.BordersEdge.xlRight);
                excelAccess.SetBordersEdge(r, GoldPrinter.ExcelConstants.BordersEdge.xlBottom);
                excelAccess.SetBordersEdge(r, GoldPrinter.ExcelConstants.BordersEdge.xlInsideHorizontal);
                excelAccess.SetBordersEdge(r, GoldPrinter.ExcelConstants.BordersEdge.xlInsideVertical);

                FolderBrowserDialog fbd = new FolderBrowserDialog();
                if (fbd.ShowDialog() == DialogResult.OK)
                {
                    excelAccess.SaveAs(string.Format("{0}\\{1}.xls", fbd.SelectedPath, DateTime.Now.ToString("yyyyMMddHHmm")), true);
                    KeaoUtils.ShowInfoMessage("报表导出Excel成功。");
                    Process.Start(fbd.SelectedPath);
                }

            }
            catch (Exception err)
            {
                KeaoUtils.LogTxt(string.Format("日志导出失败。{0}", err.Message));
            }
            finally
            {
                excelAccess.Close();
            }
        }

        private void grid_rizhi_RowStateChanged(object sender, DataGridViewRowStateChangedEventArgs e)
        {
            e.Row.HeaderCell.Value = string.Format("{0}", e.Row.Index + 1);
        }
    }
}
